﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;



using Meqs.Entity;
using Meqs.Logic.Interface;

namespace Meqs.Logic.Implement
{
    public class StuExamDetailLogic : CommonLogic<StuExamDetail, int>, IStuExamDetailLogic
    {
        #region Implementation of ICommonLogic<StuExamDetail, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(StuExamDetail entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(StuExamDetail entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public int Delete(int entityId)
        {
            return DeleteObject(entityId);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll()
        {
            return DeleteAllObject();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public StuExamDetail FindById(int entityId)
        {
            return FindObjectById(entityId);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount()
        {
            return FindObjectByCount();
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<StuExamDetail> FindAll()
        {
            return FindAllObject();
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<StuExamDetail> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion
        public IList<ExamsSchool> GetSchoolExamList(Hashtable ht)
        {
            try
            {
                string statementName = "StuExamDetail.FindSchoolExam";
                return ExecuteQueryForList<ExamsSchool>(statementName, ht);
            }
            catch (Exception ex)
            {

                throw;
            }
            
        }
        /// <summary>
        /// 保存纳入学分统计的成绩
        /// </summary>
        /// <param name="stuids"></param>
        /// <param name="subjectids"></param>
        /// <param name="scores"></param>
        /// <param name="clsid"></param>
        /// <param name="school"></param>
        /// <param name="teacherid"></param>
        public void SaveStudentScores(int[] stuids, int[] subjectids, double[] scores, int clsid, SchoolInfo school, int teacherid,int part)
        {
            //判断有没有提交本学期的成绩
            Hashtable table = new Hashtable();
            table.Add("ClassID", clsid);
            table.Add("CauseID", subjectids[0]);
            table.Add("SchoolYear", school.SchoolYear);
            table.Add("Term", school.SchoolTerm);
            table.Add("SchoolMark", school.SchoolMark);
            table.Add("Part", part);
            IList<ClassResultExamList> cre = FindCustomer<ClassResultExamList>("ClassResultExamList.Find", table);
            int listid = 0;
            if (cre.Count > 0)
            {
                listid = cre[0].RID;
            }
            else
            {
                ClassResultExamList cr = new ClassResultExamList();
                cr.Part = part;
                cr.CauseID = subjectids[0];
                cr.ClassID = clsid;
                cr.SchoolMark = school.SchoolMark;
                cr.SchoolYear = school.SchoolYear;
                cr.Term = school.SchoolTerm;
                cr.TeacherID = teacherid;
                cr.Status = 0;
                listid = (int)ExecuteInsert("ClassResultExamList.Insert", cr);
            }
            if (listid!=0)
            {
                table = new Hashtable();
                table.Add("ClassID", clsid);
                table.Add("CauseID", subjectids[0]);
                table.Add("TypeID", listid);
                table.Add("SchoolMark", school.SchoolMark);
                ExecuteDelete("StuExamDetail.Delete", table);

                for (int i = 0; i < stuids.Length; i++)
                {
                    StuExamDetail st = new StuExamDetail();
                    st.CauseID = subjectids[0];
                    st.ClassID = clsid;
                    st.SchoolMark = school.SchoolMark;
                    if (scores[i] != -1)
                    {
                        st.Score =  scores[i];
                    }
                    
                    st.StudentID = stuids[i];
                    st.TypeID = listid;
                    Create(st);
                }
            }
        }
    }
}